<?php
class Mailbox extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('mailbox_model', 'mailbox');
        $this->load->model('user_model', 'user');
    }
    
    /**
     * topmenu公告
     */
    public function announcement()
    {
        $data['announcement'] = $this->mailbox->findByParam(array('user_type'=>$this->userType, 'type'=>2, 'limit'=>5))->result();
        $jsonData = json_encode(array(
            'announcement' => $this->load->view('mailbox/announcement', $data, true)
        ));
        $callback = $this->input->get('callback');
        echo $callback . '(' . $jsonData . ')';
        exit;
    }
    
    /**
     * 站内信 - 已发
     * @param number $pg
     */
    public function sendMessage($pg = 1)
    {
        $pageNum = 15;
        $num = ($pg-1)*$pageNum;
        $params = array('fid'=>$this->uid, 'type'=>1);
        $config['base_url'] = base_url('mailbox/sendMessage');
        $config['total_rows'] = $this->mailbox->total($params);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->mailbox->page_list($pageNum, $num, $params);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $pageNum;
        $this->load->view('mailbox/sendMessage', $data);
    }
    
    /**
     * 站内信 - 已发详情
     * @param number $pg
     */
    public function sendDetail($id)
    {
        $result = $this->mailbox->findById($id);
        if ($result->num_rows() <= 0) {
            $this->redirect('mailbox/sendMessage');
        }
        $data['detail'] = $result->row();
        $this->load->view('mailbox/sendDetail', $data);
    }
    
     /**
     * 站内信 - 已收
     * @param number $pg
     */
    public function receiveMessage($pg = 1)
    {
        $pageNum = 15;
        $num = ($pg-1)*$pageNum;
        $config['base_url'] = base_url('mailbox/receiveMessage');
        $config['total_rows'] = $this->mailbox->total(array('sid'=>$this->uid, 'type'=>1));
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->mailbox->page_list($pageNum, $num, array('sid'=>$this->uid, 'type'=>1));
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $pageNum;
        $this->load->view('mailbox/receiveMessage', $data);
    }
    
    /**
     * 站内信 - 已收详情
     * @param number $pg
     */
    public function receiveDetail($id)
    {
        $result = $this->mailbox->findById($id);
        if ( $result->num_rows() <= 0 ) {
            $this->redirect('mailbox/receiveMessage');
        }

        if ( $this->input->get('read') != 1 ){
            $read = $this->mailbox->updateRead($id);
        }
        $data['detail'] = $result->row();
        $this->load->view('mailbox/receiveDetail', $data);
    }
    
    /**
     * 系统消息
     * @param number $pg
     */
    public function system($pg = 1)
    {
        $pageNum = 15;
        $num = ($pg-1)*$pageNum;
        $config['base_url'] = base_url('mailbox/system');
        $config['total_rows'] = $this->mailbox->total(array('user_type'=>$this->userType, 'type'=>2));
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->mailbox->page_list($pageNum, $num, array('user_type'=>$this->userType, 'type'=>2));
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $pageNum;
        $this->load->view('mailbox/system', $data);
    }
    
    /**
     * 站内信 - 已收详情
     * @param number $pg
     */
    public function systemDetail($id)
    {
        $result = $this->mailbox->findById($id);
        if ($result->num_rows() <= 0) {
            $this->redirect('mailbox/system');
        }
        $data['detail'] = $result->row();
        $this->load->view('mailbox/systemDetail', $data);
    }

    /**
     * 已收信息
     * @param number $pg
     */
    public function receiveBox($pg=1)
    {
        $getData = $this->input->get();
        $getData['sid'] = $this->userType;
        $getData['type'] = 1;
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('mailbox/receiveBox').$this->pageGetParam($this->input->get());;
        $config['suffix'] = $this->pageGetParam($this->input->get());;
        $config['base_url'] = base_url('mailbox/receiveBox');
        $config['total_rows'] = $this->mailbox->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->mailbox->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['flag'] = 2;
        $this->load->view('mailbox/receiveBox', $data);
    }

    public function show($pg=1)
    {
        $flag = $this->uri->segment(3);
        $id = $this->uri->segment(4);
        if ($id==NULL) {
            $this->error('mail/receiveBox', '', '错误的id');
        }
        $res = $this->mailbox->findById($id);
        if ($res->num_rows() > 0) {
            $row= $res->row();
            $data['result']=$row;

            //未读变已读
            if ($flag==2) {
                if ($row->attribute==1) {
                    $this->mailbox->update(['attribute' => 0], ['id' => $row->id]);
                }
            }
        }

        $data['flag'] = $flag;
        $this->load->view('mailbox/show', $data);
    }

    public function add($pg = 1)
    {
        $this->load->view('mailbox/add');
    }

    /**
     * 已发信息
     * @param number $pg
     */
    public function sendBox($pg=1)
    {
        $getData = $this->input->get();
        $getData['fid'] = $this->uid;
        $getData['type'] = 1;
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('mailbox/receiveBox').$this->pageGetParam($this->input->get());;
        $config['suffix'] = $this->pageGetParam($this->input->get());;
        $config['base_url'] = base_url('mailbox/receiveBox');
        $config['total_rows'] = $this->mailbox->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->mailbox->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['flag'] = 1;
        $this->load->view('mailbox/sendBox', $data);
    }

    /**
     * 系统消息
     * @param number $pg
     */
    public function systemMessage($pg=1)
    {
        $getData = $this->input->get();
        $getData['sid'] = $this->userType;
        $getData['type'] = 2;
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('mailbox/systemMessage').$this->pageGetParam($this->input->get());;
        $config['suffix'] = $this->pageGetParam($this->input->get());;
        $config['base_url'] = base_url('mailbox/systemMessage');
        $config['total_rows'] = $this->mailbox->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->mailbox->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
        $data['flag'] = 1;
        $this->load->view('mailbox/systemMessage', $data);
    }
    
    /**
     * 订单消息
     * @param number $pg
     */
    public function orderMessage($pg=1)
    {
        $getData = $this->input->get();
        $getData['sid'] = $this->userType;
        $getData['type'] = 4;
        $page_num = 15;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('mailbox/orderMessage').$this->pageGetParam($this->input->get());;
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('mailbox/orderMessage');
        $config['total_rows'] = $this->mailbox->total($getData);
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->mailbox->page_list($page_num, $num, $getData);
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['pg_num'] = $page_num;
		$data['flag'] = 1;
        $this->load->view('mailbox/orderMessage', $data);
    }
    
    /*
     * 站内信-写信显示页
     */
    public function writeMessage()
    {
        $this->load->view('mailbox/writeMessage');
    }
    
    /*
     * 站内信-写信提交
     */
    public function writePost()
    {
        $validate = $this->validateMessage();
        $postData = $this->input->post();
        if(!empty($validate)){
            $this->error('mailbox/writeMessage', '', $validate);
        }
        $postData['type'] = 1;
        $postData['fname'] = $this->userName;
        $postData['fid'] = $this->uid;
        $id = $this->mailbox->insertMailbox($postData);
        if (!$id) {
            $this->error('mailbox/writeMessage', '', '发送失败');
        }
        $this->success('mailbox/sendMessage', '', '发送成功');
    }
    
    private function validateMessage()
    {
        $error = array();
        if ($this->validateParam($this->input->post('sname')))
        {
            $error[] = '请输入收件人';
        } else {
            if($this->input->post('sname') == $this->userName){
                $error[] = '不能自己发送给自己';
            }else{
                $result = $this->validateUserName($this->input->post('sname'));
                if(!$result){
                    $error[] = '请输入正确的收件人';
                }else{
                    $_POST['sid'] = $result->uid;
                }
            }
        }

        if($this->validateParam($this->input->post('title')))
        {
            $error[] = '请输入主题';
        }

        if($this->validateParam($this->input->post('content')))
        {
            $error[] = '请输入正文';
        }
        return $error;
    }

    /**
     * 验证用户名是否存在
     * @param $userName
     * @param string $format
     * @return bool
     */
    private function validateUserName($userName, $format = 'php')
    {
        $result = $this->user->validateUserName($userName);
        if($result->num_rows() > 0){
            if($format == 'json'){
                return true;
            }
            return  $result->row();
        }
        return false;
    }

    /**
     * 验证用户是否存在
     */
    public function ajaxValidateName()
    {
        $result = $this->validateUserName($this->input->post('userName'), 'json');
        echo json_encode(array(
            'status' => $result
        ));
        exit;
    }

    public function deleteMessage($id)
    {
        $result = $this->mailbox->delete($id);
        if ($result) {
            $this->error('mailbox/sendMessage', '', '删除失败');
        }
        $this->success('mailbox/sendMessage', '', '删除成功');
    }
}